package com.hns.can.safety.web;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.service.CommonInitParamService;
import com.hns.can.common.vo.CommonParamsVo;
import com.hns.can.common.web.BaseController;
import com.hns.can.master.entity.CommMftAlmEntity;
import com.hns.can.safety.service.RealTimeFailureService;
import com.hns.can.safety.vo.RealTimeFailureParamVo;
import com.hns.can.safety.vo.RepairTodayFaultVo;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.export.ExportUtil;
import com.hns.tool.pub.DateTool;
import com.hns.tool.pub.PubUtil;
import com.hns.tool.pub.SearchConditionVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

/**
 * 实时故障Controller
 * @author:zhangzq
 * @email:
 * @version Revision 2.0.0
 */
@Controller
@RequestMapping("/realTimeFailure")
public class RealTimeFailureController extends BaseController {

    final private String[] headerNames = {"所属机构","运营线路",  "车牌号", "车辆编号", "ECU节点","故障名称", "车速(km/h)", "转速(r/min)", "发生时间", "最后上报时间", "发生站点","发生地点详细位置", "处理状态", "发生频率"};
    final private String[] headerFields = {"organName", "lineName","licPltNo", "carInCd",  "ecuMftName","mftTypeName","spdOfMotVhi", "rtteSpd", "mftOccurTime", "mftRpaTime", "locate","gpsDetailAdr", "status","mftOccurFreq" };

    @Autowired
    private RealTimeFailureService realTimeFailureService;
    @Autowired
    private CommonInitParamService commonInitParamService;

    /**
     * 分页查询实时故障数据
     * @param vo
     * @return
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/searchRealTimeFaults")
    @ResponseBody
    public Object searchRealTimeFaults(RealTimeFailureParamVo vo)throws Exception{
        SysUserEntity user = this.getCurrentUser().getUser();
        commonInitParamService.initParamsLimitCar(vo,user, Arrays.asList("T_COMM_MFT_ALM"));
        Pagination page = realTimeFailureService.getRealTimeFailure(vo);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }

    /**
     * 导出实时故障数据Excel文档
     * @param vo
     * @param response
     * @return
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/exportRealTimeFailExcel")
    public void exportRealTimeFailExcel(RealTimeFailureParamVo vo,HttpServletResponse response) throws Exception {
        List<CommMftAlmEntity> list =getRealTimeFailData(vo);
        ExportUtil.exportFiles(Arrays.asList(headerNames), Arrays.asList(headerFields), list, response, "excel", URLEncoder.encode("车辆故障明细", "UTF-8") + DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "车辆故障明细数据");
    }

    /**
     * 导出实时故障数据Pdf文档
     * @param vo
     * @param response
     * @return
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/exportRealTimeFailPdf")
    public void exportRealTimeFailPdf(RealTimeFailureParamVo vo,HttpServletResponse response) throws Exception {
        List<CommMftAlmEntity> list =getRealTimeFailData(vo);
        ExportUtil.exportFiles(Arrays.asList(headerNames), Arrays.asList(headerFields), list, response, "pdf", URLEncoder.encode("车辆故障明细", "UTF-8") + DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "车辆故障明细数据");
    }

    /**
     * 获取实时故障数据用于导出
     * @param vo
     * @return
     * @author:zhangzq
     * @email:
     */
    private List<CommMftAlmEntity> getRealTimeFailData(CommonParamsVo vo)throws Exception{
        SysUserEntity user = this.getCurrentUser().getUser();
        commonInitParamService.initParamsLimitCar(vo,user, Arrays.asList("T_COMM_MFT_ALM"));
        List<CommMftAlmEntity> list = realTimeFailureService.getAllRealTimeFailure(vo);
        if (PubUtil.isEmpty(list)) {
            throw new BusinessException(MsgType.WARN.getFlag(), "没有数据导出！");
        }
        return list;
    }
    
    
    /**
     * 分页查询实时故障数据
     * @param vo
     * @return
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/getItemFault")
    @ResponseBody
    public Object getItemFault(RealTimeFailureParamVo vo)throws Exception{
        SysUserEntity user = this.getCurrentUser().getUser();
        vo.setCorpId(user.getCorpId());
        Pagination page = realTimeFailureService.getRealTimeFailure(vo);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }
    
    /**
     * 分页查询实时故障数据
     * @param vo
     * @return
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/getRealTimeFaultDetail")
    @ResponseBody
    public Object getRealTimeFaultDetail(RealTimeFailureParamVo vo)throws Exception{
        SysUserEntity user = this.getCurrentUser().getUser();
        commonInitParamService.dateType2DateTime(vo, vo.getDateType());
        commonInitParamService.initParamsLimitCar(vo,user, Arrays.asList("T_COMM_MFT_ALM"));
        Pagination page = realTimeFailureService.getRealTimeFailure(vo);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }

    @RequestMapping("/manualRepirTodayFault")
    @ResponseBody
    public Object manualRepirTodayFault(RepairTodayFaultVo vo)throws Exception {
        realTimeFailureService.manualRepirTodayFault(vo);
        return getReturnResult("修复完成");
    }

    @RequestMapping("/manualRepirFault")
    @ResponseBody
    public Object manualFault(SearchConditionVo vo)throws Exception {
        realTimeFailureService.manualFault(vo);
        return getReturnResult("修复完成");
    }
}
